package com.cskaoyan.shopping.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.shopping.dto.*;

import com.cskaoyan.shopping.form.CartForm;
import com.cskaoyan.shopping.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

import java.util.List;
import java.util.Map;

/**
 * @author LiangRuiJiang
 * @since 2022/06/17  17:13
 */
@RestController
public class ShoppingCartController {


    @Autowired
    ICartService iCartService;

    @GetMapping("/shopping/carts")
    public ResponseData getCartsList(HttpServletRequest httpRequest) {
        // 获取请求头
        String userInfo = httpRequest.getHeader("user_info");
        // 获取json对象
        JSONObject object = JSON.parseObject(userInfo);
        // 获取uid
        Long uid = Long.parseLong(object.getString("uid").toString());

        CartListByIdRequest cartListByIdRequest = new CartListByIdRequest();
        cartListByIdRequest.setUserId(uid);
        CartListByIdResponse response = iCartService.getCartListById(cartListByIdRequest);

        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            return new ResponseUtil().setData(response.getCartProductDtos());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    @PostMapping("/shopping/carts")
    public ResponseData addProductToCart(@RequestBody() CartForm cartForm) {
        AddCartRequest request = new AddCartRequest();
        request.setUserId(cartForm.getUserId());
        request.setItemId(cartForm.getProductId());
        request.setNum(cartForm.getProductNum());
        AddCartResponse response = iCartService.addToCart(request);
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            return new ResponseUtil().setData(ShoppingRetCode.SUCCESS.getMessage());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }


    @PutMapping("/shopping/carts")
    public ResponseData updateShoppingCart(@RequestBody() CartForm cartForm) {
        UpdateCartNumRequest request = new UpdateCartNumRequest();
        request.setUserId(cartForm.getUserId());
        request.setItemId(cartForm.getProductId());
        request.setNum(cartForm.getProductNum());
        request.setChecked(cartForm.getChecked());
        UpdateCartNumResponse response = iCartService.updateCartNum(request);
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            return new ResponseUtil().setData(ShoppingRetCode.SUCCESS.getMessage());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());

    }

    /**
     * @Description:
     * @param null
     * @return:
     * @Author: 蒋镇泽
     * @Date: 2022/6/18 12:25
     */
    @Autowired
    IHomeService iHomeService;

    @GetMapping("/shopping/homepage")
    public ResponseData homepage() {
        HomePageResponse response = iHomeService.queryAll();
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            return new ResponseUtil().setData(response.getPanelContentItemDtos());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    @Autowired
    IContentService iContentService;

    @GetMapping("/shopping/navigation")
    public ResponseData navigation() {
        NavListResponse response = iContentService.queryNavList();
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            return new ResponseUtil().setData(response.getPannelContentDtos());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }



    @Autowired
    IProductCateService iProductCateService;

    @GetMapping("/shopping/categories")
    public ResponseData categories() {
        //不知道request里面写啥,目前来看是遍历,后面有新需求在优化吧
        AllProductCateRequest request = new AllProductCateRequest();
        AllProductCateResponse response = iProductCateService.getAllProductCate(request);
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            return new ResponseUtil().setData(response.getProductCateDtoList());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());

    }

    @Autowired
    ICartService cartService;



    @DeleteMapping("/shopping/carts/{uid}/{pid}")
    public ResponseData deleteCartProduct(@PathVariable("uid") Long uid, @PathVariable("pid") Long pid) {
        DeleteCartItemRequest deleteCartItemRequest = new DeleteCartItemRequest();
        deleteCartItemRequest.setUserId(uid);
        deleteCartItemRequest.setItemId(pid);
        DeleteCartItemResponse deleteCartItemResponse = cartService.deleteCartItem(deleteCartItemRequest);
        if (ShoppingRetCode.SUCCESS.getCode().equals(deleteCartItemResponse.getCode())) {
            // 执行成功，直接返回前端需要的数据
            return new ResponseUtil().setData(ShoppingRetCode.SUCCESS.getMessage());
        }
        // 调用失败
        return new ResponseUtil().setErrorMsg(deleteCartItemResponse.getMsg());
    }

    @DeleteMapping("/shopping/items/{uid}")
    public ResponseData deleteCheckedProduct(@PathVariable("uid") Long uid) {
        DeleteCheckedItemRequest deleteCheckedItemRequest = new DeleteCheckedItemRequest();
        deleteCheckedItemRequest.setUserId(uid);
        DeleteCheckedItemResposne deleteCheckedItemResposne = cartService.deleteCheckedItem(deleteCheckedItemRequest);
        if (ShoppingRetCode.SUCCESS.getCode().equals(deleteCheckedItemResposne.getCode())) {
            // 执行成功，直接返回前端需要的数据
            return new ResponseUtil().setData(deleteCheckedItemResposne.getMsg());
        }
        // 调用失败
        return new ResponseUtil().setErrorMsg(deleteCheckedItemResposne.getMsg());
    }

    @PutMapping("/shopping/items")
    public ResponseData checkAllProducts(@RequestBody Map map) {
        CheckAllItemRequest checkAllItemRequest = new CheckAllItemRequest();
        String userId = (String) map.get("userId");
        Boolean checked = (Boolean) map.get("checked");
        checkAllItemRequest.setChecked(checked.toString());
        checkAllItemRequest.setUserId(Long.parseLong(userId));
        CheckAllItemResponse checkAllItemResponse = cartService.checkAllCartItem(checkAllItemRequest);
        if (ShoppingRetCode.SUCCESS.getCode().equals(checkAllItemResponse.getCode())) {
            // 执行成功，直接返回前端需要的数据
            return new ResponseUtil().setData(checkAllItemResponse.getMsg());
        }
        // 调用失败
        return new ResponseUtil().setErrorMsg(checkAllItemResponse.getMsg());
    }

}

